package com.leps.nq.op.controller;

import cn.hutool.core.bean.BeanUtil;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.util.JSONUtils;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.entity.OpRetire;
import com.leps.nq.op.service.IOpRetireService;
import com.leps.nq.op.vo.CreateRetireVo;
import com.leps.nq.op.vo.RetireQueryModel;
import com.leps.nq.op.vo.UpdateRetireVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 失效记录表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/op/op-retire")
@Api(tags = "失效记录管理")
@Slf4j
public class OpRetireController {
    @Autowired
    IOpRetireService retireService;

    @PostMapping
    @ApiOperation(value = "新增失效记录", notes = "注意：上传文件时单独请求文件上传接口")
    @Log(title = "失效记录", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody CreateRetireVo vo) {
        OpRetire entity = BeanUtil.toBean(vo, OpRetire.class);
        String pic = JSONUtils.toJson(vo.getPicFiles());
        entity.setPic(pic);
        this.retireService.save(entity);
        return RBuilder.ok(entity);
    }

    @ApiOperation(value = "修改失效记录", notes = "注意：上传文件时单独请求文件上传接口")
    @PutMapping
    @Log(title = "失效记录", businessType = BusinessType.UPDATE)
    public R update(@RequestBody UpdateRetireVo vo) {
        OpRetire entity = BeanUtil.toBean(vo, OpRetire.class);
        entity.setPic(JSONUtils.toJson(vo.getPicFiles()));
        this.retireService.updateById(entity);
        return RBuilder.ok(entity);
    }

    @DeleteMapping
    @ApiOperation("删除失效记录")
    @Log(title = "失效记录", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Long> retireIds) {
        boolean b = this.retireService.removeBatchByIds(retireIds);
        return RBuilder.ofBoolean(b);
    }

    @GetMapping
    @ApiOperation("失效记录列表")
    public R<PageVo<OpRetire>> list(RetireQueryModel queryModel) {
        PageVo<OpRetire> pageVo = this.retireService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询失效记录")
    public R<OpRetire> getById(@PathVariable Long id) {
        return RBuilder.ok(this.retireService.getById(id));
    }

}
